import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from "path";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react({
      babel: {
        // 配置 babel-plugin-import 实现按需加载
        plugins: [
          [
            "import",
            {
              libraryName: "antd",
              libraryDirectory: "es", // 使用 ES 模块
              style: "css", // 加载 CSS 样式（也可设置为 true 加载 less 源码）
            },
            "antd", // 命名空间，避免与其他插件冲突
          ],
        ],
      },
    }),
  ],
  build: {
    rollupOptions: {
      input: {
        popup: resolve(".", "index.html"),
      },
      output: {
        entryFileNames: "[name].js",
        chunkFileNames: "[name].js",
        assetFileNames: "[name].[ext]",
        manualChunks: {
          // 将 Ant Design 单独打包
          antd: ["antd"],
          // 将 React 相关库单独打包
          react: ["react", "react-dom"],
        },
      },
    },
    outDir: "dist",
    emptyOutDir: true,
    // 调整chunk大小警告阈值
    chunkSizeWarningLimit: 1000,
    // 启用压缩
    minify: "terser",
  },
  base: "./",
  define: {
    global: "globalThis",
  },
});
